package com.uchengwang.ui.cm_ui.cycle.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.ui.base_ui.ui.BaseWhitTitleNoBackActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleThemeBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.ImageManagerUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/5/26.
 * 创建社圈提交
 */

public class SubmitCreateCycleActivity extends BaseWhitTitleNoBackActivity implements View.OnClickListener{
    private RelativeLayout submit_name_rl, submit_iden_rl;
    private TextView submit_name, submit_iden, submit_state;
    private AppCompatCheckBox submit_checkBox;
    private Button submit_sure;

    private String realNameString, inviteCodeString, themeName, cycleName, cycleSign, cycleCoverLocal, cycleCoverOss;
    private CycleThemeBean cycleThemeBean;
    private boolean isAllow = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_submit_create_cycle);
        inviteCodeString = getIntent().getStringExtra(BaseConfig.INVITE_CODE);
        cycleThemeBean = (CycleThemeBean) getIntent().getSerializableExtra(BaseConfig.BEAN);
        cycleName = getIntent().getStringExtra(BaseConfig.CYCLE_NAME);//社圈名
        cycleSign = getIntent().getStringExtra(BaseConfig.CYCLE_SIGN);//社圈标语
        cycleCoverLocal = getIntent().getStringExtra(BaseConfig.CYCLE_COVER);//社圈封面图

        initView();

    }

    private void initView() {
        submit_name_rl = (RelativeLayout) findViewById(R.id.submit_name_rl);
        submit_name_rl.setOnClickListener(this);

        submit_iden_rl = (RelativeLayout) findViewById(R.id.submit_iden_rl);
        submit_name = (TextView) findViewById(R.id.submit_name);
        submit_iden = (TextView) findViewById(R.id.submit_iden);
        submit_state = (TextView) findViewById(R.id.submit_state);
        submit_state.setOnClickListener(this);

        submit_checkBox = (AppCompatCheckBox) findViewById(R.id.submit_checkBox);

        submit_sure = (Button) findViewById(R.id.submit_sure);
        submit_sure.setOnClickListener(this);
        submit_sure.setEnabled(false);
        submit_sure.setBackgroundResource(R.drawable.shape_100_9);

        initData();
    }

    private void initData(){
        submit_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    submit_sure.setEnabled(true);
                    submit_sure.setBackgroundResource(R.drawable.selector_login_bg);
                }else {
                    submit_sure.setEnabled(false);
                    submit_sure.setBackgroundResource(R.drawable.shape_100_9);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.submit_name_rl://输入名字
                DialogUtils.getUtils().showEditWindow(this, "真实姓名", realNameString, 10, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (!TextUtils.isEmpty(data)){
                            realNameString = data;
                            submit_name.setText(data);
                        }
                    }
                });
                break;
            case R.id.submit_sure://激活社圈
                if (TextUtils.isEmpty(realNameString)){
                    UCommonUtil.getUtil().showToast(this, "请输入姓名");
                    return;
                }

                if (!isAllow){
                    UCommonUtil.getUtil().showToast(this, "正在提交");
                    return;
                }
                isAllow = false;
                LoadingUtils.getUtils().loading(this);
                ImageManagerUtils.getUtils().compressAndPost(this, OSSManager.OSS_SHARE, cycleCoverLocal, 300, new ImageManagerUtils.ImageCompressListener() {
                    @Override
                    public void onResult(int stutas, String msg, String result) {
                        if (!TextUtils.isEmpty(result)){
                            cycleCoverOss = result;
                            createCycle();
                        }else {
                            LoadingUtils.getUtils().hide();
                            isAllow = true;
                            UCommonUtil.getUtil().showToast(SubmitCreateCycleActivity.this, "上传失败");
                        }
                    }
                });


                break;

            case R.id.submit_state://协议界面
                Intent intent = new Intent(this, LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/static/agreement_cycle.html");
                startActivity(intent);

                break;
        }
    }

    //创建社圈
    private void createCycle(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.CYCLE_COVER, cycleCoverOss);
        map.put(BaseConfig.CYCLE_DESC, cycleSign);
        map.put(BaseConfig.CYCLE_NAME, cycleName);
        map.put(BaseConfig.INVITE_CODE, inviteCodeString);
        map.put(BaseConfig.REAL_NAME, realNameString);

        map.put(BaseConfig.CYCLE_TYPE, cycleThemeBean.getCycleType());
        map.put(BaseConfig.ITEM_ID, cycleThemeBean.getId());
        map.put(BaseConfig.ITEM_NAME, cycleThemeBean.getItemName());

        CycleHttpUtils.getUtils().createCycle(this, map, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                LoadingUtils.getUtils().hide();
                if (stutas ==0){
                    UCommonUtil.getUtil().showToast(SubmitCreateCycleActivity.this, "激活成功");
                    EventBus.getDefault().post(BaseConfig.RELEASE_CYCLE_SUCCESS);
                    SubmitCreateCycleActivity.this.finish();
                }else {
                    isAllow = true;
                    UCommonUtil.getUtil().showToast(SubmitCreateCycleActivity.this, msg);
                }
            }
        });
    }
}
